//
//  XWPickerView.m
//  XNEducation
//
//  Created by zxw on 2020/3/26.
//  Copyright © 2020 zxw. All rights reserved.
//

#import "XWPickerView.h"
#import "SPAlertController.h"
#import <Masonry/Masonry.h>

#define XNMinePicker_MaxAddCount 1

//--------------------------ABOUT COLOR 颜色相关 -------------------------
//RGB转UIColor（不带alpha值）
#define UIColorFromRGB(rgbValue) [UIColor  colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0  green:((float)((rgbValue & 0xFF00) >> 8))/255.0  blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//RGB转UIColor（带alpha值）
#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor  colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0  green:((float)((rgbValue & 0xFF00) >> 8))/255.0  blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]

@interface XWPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)UIPickerView *pickerView;
@property(nonatomic,strong)NSArray      *originalArray;
@property(nonatomic,strong)NSArray      *dataSouce;
@property(nonatomic,assign)NSInteger    selectRow;

@property(nonatomic,strong) UIViewController *currentController;

@end

@implementation XWPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        self.backgroundColor = UIColorFromRGB(0xFAFAFA);
        
        UIView *topBgView = [[UIView alloc]init];
        topBgView.backgroundColor = UIColorFromRGB(0xE5E5E5);
        [self addSubview:topBgView];
        [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.height.mas_equalTo(50);
        }];
        
        self.cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.cancleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.cancleBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        [self.cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [topBgView addSubview:self.cancleBtn];
        [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(60);
        }];
        [self.cancleBtn addTarget:self action:@selector(cancleBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.sureBtn setTitleColor:UIColorFromRGB(0x54C377) forState:UIControlStateNormal];
        [self.sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [topBgView addSubview:self.sureBtn];
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(60);
        }];
        [self.sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.pickerView];
        [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(topBgView.mas_bottom);
        }];
        
    }
    return self;
}

-(void)reloadDataWithArray:(NSArray*)itemStrs{
    
    self.originalArray = itemStrs;
    NSMutableArray *mutArr = [[NSMutableArray alloc]init];
    for (int i = 0; i<XNMinePicker_MaxAddCount; i++) {
        [mutArr addObjectsFromArray:itemStrs];
    }
    self.dataSouce = mutArr;
    self.selectRow = XNMinePicker_MaxAddCount/2*itemStrs.count;
    [self.pickerView selectRow:self.selectRow inComponent:0 animated:NO];
    
}

#pragma mark - dataSouce
//有几行
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

//行中有几列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataSouce.count;
}
//列显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
    return self.dataSouce[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

- (UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view{
    
    //设置分割线的颜色
    for(UIView*singleLine in pickerView.subviews){
        if(singleLine.frame.size.height<1){
            singleLine.backgroundColor= [UIColor clearColor];
        }
    }
    
    //设置文字的属性
    UILabel *titleLabel = [UILabel new];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.text=self.dataSouce[row];
    titleLabel.textColor = UIColorFromRGB(0x4A4A4A);
    
    if (row == self.selectRow) {
        titleLabel.font = [UIFont boldSystemFontOfSize:20];
    }
 
    return titleLabel;
}


#pragma mark - delegate
// 选中某一组的某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *selFood = self.dataSouce[row];
    NSLog(@"%@", selFood);
    self.selectRow = row;
    [pickerView reloadAllComponents];
}


-(void)cancleBtnAction{
    [self.currentController dismissViewControllerAnimated:YES completion:nil];
}

-(void)sureBtnAction{
    
    if (self.didClickSureBtnBlock) {
        NSInteger index = self.selectRow%self.originalArray.count;
        self.didClickSureBtnBlock(self.dataSouce[self.selectRow],index);
    }
    
    [self cancleBtnAction];
}

-(void)showWithViewController:(UIViewController*)controller{
    
    SPAlertController *alertController = [SPAlertController alertControllerWithCustomAlertView:self preferredStyle:SPAlertControllerStyleActionSheet animationType:SPAlertAnimationTypeFromBottom];
    alertController.cornerRadius = 0;
    [alertController setBackgroundViewAppearanceStyle:SPBackgroundViewAppearanceStyleTranslucent alpha:0];
    self.currentController = alertController;
    [controller presentViewController:alertController animated:YES completion:nil];
    
}


#pragma mark - lazy loading

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.showsSelectionIndicator = NO;
    }
    return _pickerView;
    
}


@end
